package com.yzc.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.yzc.model.entity.AliOSS.AliOSSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;

@Service
public class OssService {
    @Autowired
    private AliOSSProperties aliOSSProperties;

    public void uploadFile(String objectName, byte[] fileContent) {
        // 创建OSS客户端实例
        OSS ossClient = new OSSClientBuilder().build(aliOSSProperties.getEndpoint(),
                aliOSSProperties.getAccessKeyId(), aliOSSProperties.getAccessKeySecret());
        try {

            ossClient.putObject(aliOSSProperties.getBucketName(), objectName, new ByteArrayInputStream(fileContent));
        } finally {

            ossClient.shutdown();
        }
    }
}